package pl.shenlon.poker.gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;

public class AboutDialog extends JDialog {

	private JLabel aboutLabel;
	private JScrollPane aboutLabelScrollPane;
	public AboutDialog() {
		initComponent();
		pack();
		setLocationRelativeTo(null);

		getRootPane().registerKeyboardAction(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);

			}
		}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		// setSize(350, 300);
	}

	private void initComponent() {
		Container contentPane = getContentPane();
		contentPane.setLayout(new MigLayout("fill"));
		aboutLabel = new JLabel();
		aboutLabel.setHorizontalAlignment(SwingConstants.LEFT);
		aboutLabel.setVerticalAlignment(SwingConstants.TOP);
		aboutLabelScrollPane = new JScrollPane(aboutLabel);

		contentPane.add(aboutLabelScrollPane, "grow");
	}

	public void setContent(File content) {
		StringBuilder sb = new StringBuilder();
		Scanner in = null;
		try {
			in = new Scanner(content, "UTF-8");
			while (in.hasNextLine()) {
				sb.append(in.nextLine());
			}
			in.close();
		} catch (IOException ex) {

		} finally {
			if (in != null) {
				in.close();
			}
		}
		aboutLabel.setText(sb.toString());
		sb.setLength(0);
		pack();
		setLocationRelativeTo(null);
	}

	public void setContent(InputStream content) {
		StringBuilder sb = new StringBuilder();
		Scanner in = null;
		in = new Scanner(content, "UTF-8");
		while (in.hasNextLine()) {
			sb.append(in.nextLine());
		}
		in.close();

		aboutLabel.setText(sb.toString());
		sb.setLength(0);
		pack();
		setLocationRelativeTo(null);
	}

	public void setContent(String content) {
		aboutLabel.setText(content);
	}
}